<template>
  <div class="loginBox">
    <span class="title">面试指南平台</span>
    <div class="inputBox">
      <el-form :rules="rules" ref="formData" :model="formData">
        <el-form-item prop="userEmail">
          <el-input v-model="formData.userEmail" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            class="password"
            placeholder="请输入密码"
            type="password"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="loginButton"
            type="primary"
            @click="login('formData')"
            >登录</el-button
          >
          <div class="footer">
            <div class="autoLogin">
              <span class="registText" @click="goRegister"
                >没有账号？去注册</span
              >
            </div>
            <div class="registOrForget">
              <span class="forgetPwd" @click="goForgetPwd">忘记密码</span>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { checkEmail, debounce } from '@/utils/user.utils'
export default {
  name: 'LoginBox',
  data() {
    const validatorUserEmail = (rule, value, callback) => {
      if (!value.trim()) callback(new Error('请输入您的邮箱账号'))
      if (!checkEmail(value)) callback(new Error('请输入正确的邮箱格式'))
      callback()
    }
    //TODO 简写
    const validatorPassword = (rule, value, callback) => {
      if (!value.trim()) callback(new Error('请输入您的密码'))
      callback()
    }
    return {
      formData: { userEmail: '', password: '' },
      rules: {
        userEmail: [
          { required: true, validator: validatorUserEmail, trigger: 'blur' }
        ],
        password: [
          { required: true, validator: validatorPassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击登录按钮
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) this.$emit('login', this.formData)
        return
      })
    },
    // 点击注册按钮
    goRegister() {
      this.$router.push('/register')
    },
    // 点击忘记密码按钮
    goForgetPwd() {
      this.$message({
        message: '开发中...'
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.loginBox {
  box-sizing: border-box;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 500px;
  height: 456px;
  border-radius: 10px;
  box-shadow: 0 0 4px #ccc;
  background-color: #fff;
  padding: 50px 80px;
  text-align: center;
  .title {
    font-size: 22px;
    letter-spacing: 3px;
  }
  .inputBox {
    margin-top: 50px;
    .password {
      margin-top: 10px;
    }
  }
  .loginButton {
    height: 50px;
    margin-top: 20px;
    width: 100%;
  }
  .footer {
    margin-top: 10px;
    font-size: 13px;
    color: #999;
    overflow: hidden;
    .autoLogin {
      float: left;
      .registText {
        cursor: pointer;
        &:hover {
          color: #00aae6;
        }
      }
    }
    .registOrForget {
      float: right;
      .forgetPwd {
        cursor: pointer;
        &:hover {
          color: #00aae6;
        }
      }
    }
  }
}
</style>
